const db = require('./db');

// 简单的base64格式图片示例（简化版，实际数据会更长）
const sampleImages = [
  // 红色书本封面示例
  [''],
  // 蓝色书本封面示例
  [''],
  // 绿色书本封面示例
  [''],
  // 多图示例
  [
    '',
    ''
  ]
];

// 示例教材数据
const sampleTextbooks = [
  {
    title: 'JavaScript高级程序设计',
    author: 'Matt Frisbie',
    publisher: '人民邮电出版社',
    isbn: '9787115521682',
    category_id: 1,
    seller_id: 1,
    description: '《JavaScript高级程序设计》第4版全面介绍JavaScript语言核心，以及Web API和浏览器对象模型的相关知识。',
    price: 108.00,
    original_price: 168.00,
    condition: 'like_new',
    images: sampleImages[0],
    status: 'on_sale'
  },
  {
    title: 'Python编程：从入门到实践',
    author: 'Eric Matthes',
    publisher: '人民邮电出版社',
    isbn: '9787115428028',
    category_id: 1,
    seller_id: 1,
    description: '适合初学者的Python入门书籍，包含理论知识和实践项目。',
    price: 79.00,
    original_price: 109.00,
    condition: 'good',
    images: sampleImages[1],
    status: 'on_sale'
  },
  {
    title: '算法导论',
    author: 'Thomas H. Cormen, Charles E. Leiserson',
    publisher: '机械工业出版社',
    isbn: '9787111407010',
    category_id: 1,
    seller_id: 2,
    description: '经典算法教材，内容全面，适合深入学习算法知识。',
    price: 89.00,
    original_price: 128.00,
    condition: 'good',
    images: sampleImages[2],
    status: 'on_sale'
  },
  {
    title: '计算机网络：自顶向下方法',
    author: 'James F. Kurose, Keith W. Ross',
    publisher: '机械工业出版社',
    isbn: '9787111607952',
    category_id: 1,
    seller_id: 2,
    description: '采用自顶向下方法讲解计算机网络原理，内容清晰易懂。',
    price: 99.00,
    original_price: 139.00,
    condition: 'like_new',
    images: sampleImages[3],
    status: 'on_sale'
  },
  {
    title: '数据结构与算法分析',
    author: 'Mark Allen Weiss',
    publisher: '机械工业出版社',
    isbn: '9787111598220',
    category_id: 1,
    seller_id: 1,
    description: '详细讲解各种数据结构和算法的设计与分析。',
    price: 69.00,
    original_price: 108.00,
    condition: 'good',
    images: sampleImages[1],
    status: 'on_sale'
  }
];

// 插入示例数据的函数
async function insertSampleTextbooks() {
  try {
    console.log('开始插入示例教材数据...');
    
    // 测试数据库连接
    await db.testConnection();
    
    // 遍历示例数据并插入
    for (const textbook of sampleTextbooks) {
      const sql = `
        INSERT INTO textbooks 
        (title, author, publisher, isbn, category_id, seller_id, description, price, original_price, \`condition\`, images, status) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
      `;
      
      const values = [
        textbook.title,
        textbook.author,
        textbook.publisher,
        textbook.isbn,
        textbook.category_id,
        textbook.seller_id,
        textbook.description,
        textbook.price,
        textbook.original_price,
        textbook.condition,
        JSON.stringify(textbook.images),
        textbook.status
      ];
      
      const result = await db.promiseQuery(sql, values);
      console.log(`成功插入教材: ${textbook.title}, ID: ${result.insertId}`);
    }
    
    console.log('所有示例教材数据插入完成！');
    
  } catch (error) {
    console.error('插入示例数据时出错:', error.message);
  } finally {
    // 关闭数据库连接
    process.exit(0);
  }
}

// 执行插入操作
insertSampleTextbooks();